package org.opens.db.column.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.opens.converter.ConverterFactory;
import org.opens.converter.ConverterFactoryKey;
import org.opens.converter.mybatisJdbcType.I_MybatisJdbcTypeConverter;
import org.opens.converter.mybatisJdbcType.MybatisJdbcTypeConverterEntity;
import org.opens.converter.sqlType.I_SqlTypeConverter;
import org.opens.converter.sqlType.SqlTypeConverterEntity;
import org.opens.db.column.entity.ColumnEntity;
import org.opens.db.constant.DbType;

import java.util.*;
import java.util.stream.Collectors;

public class DbConfigUtil {

    /**
     * 根据所配置的json进行自动设置一些属性
     * @param dbType    数据库类型
     * @param columnEntities    数据库列的实体信息
     */
    public static Set<String> autoSetColumnConfig(DbType dbType, List<ColumnEntity> columnEntities) {
        Assert.notNull(dbType, "自动设置列配置必须设置数据库类型!");
        Assert.notEmpty(columnEntities, "自动设置列配置的列不能为空!");

        Set<String> importSet = new HashSet<>();

        I_SqlTypeConverter sqlTypeConverter = ConverterFactory.getInstance(ConverterFactoryKey.SQL_TYPE_CONVERTER_FACTORY)
                .getSqlTypeConverter(dbType);
        I_MybatisJdbcTypeConverter mybatisJdbcTypeConverter = ConverterFactory.getInstance(ConverterFactoryKey.MYBATIS_JDBC_TYPE_CONVERTER_FACTORY)
                .getMybatisJdbcTypeConverter(dbType);

        for (ColumnEntity columnEntity : columnEntities) {
            SqlTypeConverterEntity sqlTypeConverterEntity = sqlTypeConverter.dbType2JavaType(columnEntity.getColumnType());
            MybatisJdbcTypeConverterEntity mybatisJdbcTypeConverterEntity = mybatisJdbcTypeConverter.dbType2JdbcType(columnEntity.getColumnType());

            importSet.add(sqlTypeConverterEntity.getJavaTypeFullName());

            columnEntity.setJavaFiledName(StrUtil.toCamelCase(columnEntity.getColumnName()));

            columnEntity.setJavaTypeName(sqlTypeConverterEntity.getJavaTypeClassName());

            columnEntity.setJdbcType(mybatisJdbcTypeConverterEntity.getJdbcTypeName());
        }

        return importSet;
    }

    public static Set<String> calcImportList(Set<String>... needImportSet) {
        Set<String> importSet = new HashSet<>();

        for (Set<String> item : needImportSet) {
            importSet.addAll(item);
        }

        // java.lang.*下面的类不需要导入, 需要去掉
        Set<String> removedNoNeedImportSet = importSet.stream().filter(item -> !item.startsWith("java.lang")).collect(Collectors.toSet());

        return removedNoNeedImportSet;
    }

    /**
     * 如果有设置忽略列的画, 则将数据库中查询出的列忽略掉
     * @param columnEntityList  数据库中查询出的列信息
     * @param ignoreColumnList  待忽略的列
     */
    public static void removeIgnoreColumn(List<ColumnEntity> columnEntityList, List<String> ignoreColumnList) {
        if (CollectionUtil.isEmpty(ignoreColumnList)) {
            return;
        }

        Assert.notEmpty(columnEntityList, "param 1 can be not empty!");

        for (int i = 0; i < columnEntityList.size(); i++) {
            if (ignoreColumnList.contains(columnEntityList.get(i).getColumnName())) {
                columnEntityList.remove(i);
            }
        }
    }

}
